<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Service\YoppayUtils\YopClient3;
use App\Service\YoppayUtils\YopRequest;
use App\Service\OrderService;
use App\Common\Base;

class ConfirmUnitOrderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:ConfirmUnitOrderCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'send command to YiBao for confirm unit order currency';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //查找小程序分笔订单，已支付，交易已完成的
        echo "----------------Date:".date('Y-m-d H:i:s')."----------------\n";
        $orders = DB::table('unit_order')
                    ->where(['payment_status' => 1, 'state' => 0, 'is_confirm' => 0])
                    ->get()
                    ->toarray(); 
        $orders = array_map('get_object_vars', $orders); 

        foreach ($orders as $key => $order) 
        {
            if( $order['transaction_id'] != null &&
            (time() - strtotime($order['payment_time']) >= Config('app.settleMerchantTime') ) 
            ){
                try{
                    $orderConfirmed = OrderService::confirmUnitOrder($order['pay_batchcode']);
                    if($orderConfirmed['code'] != 666){
                        echo date('Y-m-d H:i:s')."-------分笔订单确认失败------".$order['pay_batchcode']."-------".$orderConfirmed['msg']."----".json_encode($orderConfirmed['data'])."-----\n";
                    }else{
                        echo date('Y-m-d H:i:s')."-------分笔订单确认成功------".$order['pay_batchcode']."-------".$orderConfirmed['msg']."-----\n";
                    }
                }catch(\Exception $exception) {
                    echo date('Y-m-d H:i:s').'-------分笔订单确认失败------'.$order['pay_batchcode']."-------\n";
                }
            }
            
            
         } 
    }
}
